package com.screen.screendisplay.service.impl.ne;

import com.screen.screendisplay.entity.ne.NeOverviewItem;
import com.screen.screendisplay.mapper.ne.NeOverviewItemMapper;
import com.screen.screendisplay.service.ne.NeOverviewItemService;
import com.screen.screendisplay.vo.ne.NeOverviewIndicatorsVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * <p>
 * 新能源产业主要指标表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOverviewItemServiceImpl extends ServiceImpl<NeOverviewItemMapper, NeOverviewItem> implements NeOverviewItemService {

    @Override
    public NeOverviewIndicatorsVO getMainIndicators(Integer year) {
        QueryWrapper<NeOverviewItem> queryWrapper = new QueryWrapper<>();
        
        if (year != null) {
            queryWrapper.eq("year", year);
        } else {
            // 如果没有指定年份，获取当前年份的数据
            int currentYear = LocalDate.now().getYear();
            queryWrapper.eq("year", currentYear);
        }
        
        // 按时间降序排列，获取最新的一条记录
        queryWrapper.orderByDesc("year", "month");
        queryWrapper.last("LIMIT 1");
        
        NeOverviewItem entity = this.getOne(queryWrapper);
        
        NeOverviewIndicatorsVO vo = new NeOverviewIndicatorsVO();
        if (entity != null) {
            vo.setWindFarmCount(entity.getWindFarmCount());
            vo.setWindInstalledCapacity(entity.getWindInstalledCapacity());
            vo.setWindUnderConstruction(entity.getWindUnderConstruction());
            vo.setPvStationCount(entity.getPvStationCount());
            vo.setCentralizedPvCapacity(entity.getCentralizedPvCapacity());
            vo.setDistributedPvCapacity(entity.getDistributedPvCapacity());
            vo.setPvUnderConstruction(entity.getPvUnderConstruction());
        } else {
            // 如果数据库中没有数据，返回默认值（与原mock数据一致）
            vo.setWindFarmCount(25);
            vo.setWindInstalledCapacity(new BigDecimal("1580.5"));
            vo.setWindUnderConstruction(new BigDecimal("320.8"));
            vo.setPvStationCount(18);
            vo.setCentralizedPvCapacity(new BigDecimal("890.2"));
            vo.setDistributedPvCapacity(new BigDecimal("445.6"));
            vo.setPvUnderConstruction(new BigDecimal("180.3"));
        }
        
        return vo;
    }

}
